<template>
  <div class="tab-pane fade" :id="id" role="tabpanel">
    <div class="card col-12">
      <div class="card-body">
        <button
              type="button"
              class="btn btn-primary"
              :disabled="saving"
              @click.stop="search()"
            >
              查找
            </button>
      </div>
      <div class="card-body">
        <form class="form-horizontal">
          <div class="row mb-1">
            <label class="control-label col-2 ms-auto text-end">主账号</label>
            <div class="col-4 me-auto">
              <input type="input" class="form-control" v-model.trim="mainAccount" />
            </div>
          </div>
          
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">应用id</label>
            <div class="col-4 me-auto">
              <input type="input" class="form-control" v-model.trim="appId" />
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">私钥</label>
            <div class="col-4 me-auto">
              <input
                type="input"
                class="form-control"
                v-model.trim="privateKey"
              />
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">支付宝公钥</label>
            <div class="col-4 me-auto">
              <input type="input" class="form-control" v-model.trim="alipayPublicKey" />
              <span class="text-muted small"></span>
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">同步通知地址</label>
            <div class="col-4 me-auto">
              <input type="input" class="form-control" v-model.trim="returnUrl" />
              <span class="text-muted small"></span>
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">异步通知地址</label>
            <div class="col-4 me-auto">
              <input type="input" class="form-control" v-model.trim="notifyUrl" />
              <span class="text-muted small"></span>
            </div>
          </div>
          <div class="row">
            <button
              type="button"
              class="btn btn-primary col-4 offset-4"
              :disabled="saving"
              @click.stop="save()"
            >
              保存
            </button>
          </div>
        </form>
      </div>
      <table class="table">
        <thead>
          <tr>
            <th>主账号</th>
            <th>应用ID</th>
            <th>私钥</th>
            <th>支付宝公钥</th>
            <th>同步通知地址</th>
            <th>异步通知地址</th>
            <th>创建时间</th>
            <th>修改时间</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="info in dataList" :key="info.id">
            <td>{{ info.mainAccount }}</td>
            <td>{{ info.appId }}</td>
            <td>{{ info.privateKey }}</td>
            <td>{{ info.alipayPublicKey }}</td>
            <td>{{ info.returnUrl }}</td>
            <td>{{ info.notifyUrl }}</td>
            <td>{{ info.createTime }}</td>
            <td>{{ info.lastUpdate }}</td>
            <td>
              <button type="button" @click.stop="edit(info)">修改</button>
              <button type="button" class="btn btn-danger ms-5"  @click.stop="deleteCfg(info.id)">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script>
import { searchAlipayConfig, saveAlipayConfig, deleteAlipayConfig } from "@/api/admin.js";

export default {
  props: {
    id: {
      type: String,
    },
  },
  data() {
    return {
      dataList: [],
      mainAccount: "",
      appId: "",
      privateKey: "",
      alipayPublicKey: "",
      returnUrl: "",
      notifyUrl: "",

      saving: false,
    };
  },
  mounted: function () {
    this.searchAlipayConfig();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    searchAlipayConfig: function () {
      searchAlipayConfig((v) => {
        this.dataList = v;
      });
    },
    save: function () {
      const params = {
        mainAccount: this.mainAccount,
        appId: this.appId,
        privateKey: this.privateKey,
        alipayPublicKey: this.alipayPublicKey,
        returnUrl: this.returnUrl,
        notifyUrl: this.notifyUrl,
      };

      this.saving = true;

      saveAlipayConfig(
        params,
        (v) => {
          if (v.status === "OK") {
            this.showErrMsg("保存成功");
            this.searchAlipayConfig();
          }
          else this.showErrMsg(v.errmsg, "danger");
        },
        () => {
          this.saving = false;
        }
      );
    },
    deleteCfg: function(id) {
      deleteAlipayConfig(id, v => {
        if (v.status === "OK") {
            this.showErrMsg("操作成功");
            this.searchAlipayConfig();
          }
          else this.showErrMsg(v.errmsg, "danger");
      })
    },
    edit: function (v) {
      this.mainAccount = v.mainAccount;
      this.appId = v.appId;
      this.privateKey = "";
      this.alipayPublicKey = "";
      this.returnUrl = v.returnUrl;
      this.notifyUrl = v.notifyUrl;
    },
  },
};
</script>